package com.hh.flex.config;

import com.github.pagehelper.PageInterceptor;
import com.hh.common.core.domain.BaseEntity;
import com.hh.flex.key.SnowId;
import com.hh.flex.listener.HhInsertListener;
import com.hh.flex.listener.HhUpdateListener;
import com.mybatisflex.annotation.KeyType;
import com.mybatisflex.core.FlexGlobalConfig;
import com.mybatisflex.core.keygen.KeyGeneratorFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

/**
 * Mybatis支持*匹配扫描包
 *
 * @author hh
 */
@Configuration
@MapperScan("com.hh.**.mapper")
public class MyBatisFlexConfig
{

    @Bean
    public PageInterceptor pageInterceptor(){
        PageInterceptor pageInterceptor = new PageInterceptor();
        return pageInterceptor;
    }
    @PostConstruct
    public void setFlexConfig()
    {
        FlexGlobalConfig globalConfig = FlexGlobalConfig.getDefaultConfig();
        // 关闭banner
        globalConfig.setPrintBanner(false);


        //设置数据库正常时的值
        globalConfig.setNormalValueOfLogicDelete("0");
        //设置数据已被删除时的值
        globalConfig.setDeletedValueOfLogicDelete("2");

        KeyGeneratorFactory.register("snowId", new SnowId());

        //设置全局id
        FlexGlobalConfig.KeyConfig keyConfig = new FlexGlobalConfig.KeyConfig();
        keyConfig.setKeyType(KeyType.Generator);
        keyConfig.setValue("snowId");
        keyConfig.setBefore(true);
        globalConfig.setKeyConfig(keyConfig);

        // 注册全局填充监听器
        globalConfig.registerInsertListener(new HhInsertListener(), BaseEntity.class);
        globalConfig.registerUpdateListener(new HhUpdateListener(), BaseEntity.class);
    }
}
